
const express = require('express');
const http = require('http');
const socketIo = require('socket.io');
const bodyParser = require('body-parser');
const child_process = require('child_process');

const app = express();
const ser = http.Server(app);
const soc = socketIo(ser);

// Body-parser middleware
app.use(bodyParser.urlencoded({extended:false}))
app.use(bodyParser.json())

// function to(v1,v2) {
//     return new Promise(((resolve, reject) => {
//         var workerProcess = child_process.exec("/usr/local/php/bin/php /home/wwwroot/olaindex/artisan upload tmp_2021_04_16_bvGkkXPT_Geekbench-5.4.0-Mac.zip /tmp/tmp_2021_04_16_bvGkkXPT_Geekbench-5.4.0-Mac.zip",function (error, stdout, stderr) {
//             if (error) {
//                 console.log(error.stack);
//                 console.log('Error code: '+error.code);
//                 console.log('Signal received: '+error.signal);
//             }
//             console.log('stdout: ' + stdout);
//             console.log('stderr: ' + stderr);
//         })
//         setTimeout(function () {
//
//         },ms)
//
//         workerProcess.on('exit', function (code) {
//             console.log(code)
//             console.log("process exit")
//             if(code === 0){
//                 console.log("ms:" + ms)
//                 // child_process.exec("rm "+req.body.localPath)
//
//             }else{
//                 // upload(req)
//             }
//         });
//     }))
// }
//
// to(15).then();
// // to(13);
// // to(10);

function upload(req){
    return new Promise(((resolve, reject) => {

        let cmd = "/usr/local/php/bin/php /home/wwwroot/olaindex/artisan upload "+req.body.localPath + " " + req.body.remotePath + " " + req.body.account_id
        console.log(cmd)

        var workerProcess = child_process.exec(cmd,function (error, stdout, stderr) {
            if (error) {
                console.log(error.stack);
                console.log('Error code: '+error.code);
                console.log('Signal received: '+error.signal);
            }
            console.log('stdout: ' + stdout);
            console.log('stderr: ' + stderr);
        })

        workerProcess.on('exit', function (code) {
            console.log(code)
            console.log("process exit")
            if(code === 0){
                console.log("success")
                child_process.exec("rm "+req.body.localPath)
            }else{
                console.log("上传失败")
            }
        });
    }))
}

app.get('/:socket_id/:percent', function(req, res) {
    soc.to(req.params.socket_id).emit('upload',req.params.percent);
    //socket.emit('upload',req.params.percent);
    //console.log(req.params.socket_id)
    //console.log(req.params.percent)
    res.send('success')
});

app.post('/upload', function(req, res) {
    // console.log(req.body)
    upload(req)

    res.send('success')

});

soc.on('connection',(socket)=>{
    socket.on('disconnect',()=>{
        console.log(socket.id+" disconnect");
    });
    console.log(socket.id+" connect");
    socket.on('msg',(data)=>{
        console.log(data);
        socket.emit('msg',{'socketId':socket.id});
    });


})

ser.listen(9090);
